package com.test.testcompose.page

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import com.test.testcompose.bean.ItemCardBean
import com.test.testcompose.ui.theme.TestComposeTheme

class ListActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            TestComposeTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { _ ->
                    Column {
                        // 初始化数据
                        val itemList = getSampleList()
                        ListColumn(itemList)
                    }
                }
            }
        }
    }

    @Composable
    fun ListColumn(itemList: List<ItemCardBean>) {
        LazyColumn(
            modifier =
                Modifier
                    .fillMaxSize()
                    .padding(6.dp),
        ) {
            // 使用 ItemCardBean 的 id 作为 key
            items(itemList, key = { it.id }) { item ->
                ItemCard(cardBean = item)
            }
        }
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ItemCard(cardBean: ItemCardBean) {
        Row(modifier = Modifier.padding(2.dp).clickable {
            Toast.makeText(this@ListActivity, "点击了${cardBean.title}", Toast.LENGTH_SHORT).show()
        }) {
            GlideImage(
                model = cardBean.image,
                contentDescription = null,
                modifier =
                    Modifier
                        .width(150.dp)
                        .height(80.dp),
                // 设置 contentScale 参数
                contentScale = ContentScale.Crop,
            )

            Column(modifier = Modifier.padding(12.dp, 0.dp, 8.dp, 0.dp)) {
                Text(
                    text = cardBean.title,
                    fontSize = 18.sp,
                )
                Text(
                    text = cardBean.text,
                    fontSize = 14.sp,
                )
            }
        }
    }

    // 移除错误的 itemList 参数
    @Preview(showBackground = true)
    @Composable
    fun ListColumnPreview() {
        // 提供示例数据
        val sampleList = getSampleList()
        // 调用原函数
        ListColumn(itemList = sampleList)
    }

    private fun getSampleList(): List<ItemCardBean> =
        listOf(
            ItemCardBean(
                id = "1",
                "测试1",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "2",
                "测试2",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "3",
                "测试3",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "4",
                "测试4",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
            ItemCardBean(
                id = "5",
                "测试5",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "6",
                "测试6",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "7",
                "测试7",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "8",
                "测试8",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
            ItemCardBean(
                id = "9",
                "测试9",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "10",
                "测试10",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "11",
                "测试11",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "12",
                "测试12",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
            ItemCardBean(
                id = "13",
                "测试13",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "14",
                "测试14",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "15",
                "测试15",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "16",
                "测试16",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
            ItemCardBean(
                id = "17",
                "测试17",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "18",
                "测试18",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "19",
                "测试19",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "20",
                "测试20",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
            ItemCardBean(
                id = "21",
                "测试21",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=3623219971,4266405366&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            ),
            ItemCardBean(
                id = "22",
                "测试22",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img1.baidu.com/it/u=2625021576,2385356552&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422",
            ),
            ItemCardBean(
                id = "23",
                "测试23",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://img0.baidu.com/it/u=3883580843,682629957&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            ),
            ItemCardBean(
                id = "24",
                "测试24",
                "这里是一大串的商品说明文字哈哈哈哈哈哈哈哈哈",
                "https://wx2.sinaimg.cn/mw690/bfcada65ly1hrbmsm8xovj21t43wzb2d.jpg",
            ),
        )
}
